package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils3.date.IceDateFormatUtils;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.dao.OrderCreditMonthDao;
import com.edt.ichannel.api.entiy.local.OrderCreditMonth;
import com.edt.ichannel.api.service.ComputeService;
import com.edt.ichannel.api.service.OrderCreditMonthService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * OrderCreditMonth服务接口实现类
 * @author 李爽
 * @date 2019-01-10
 */
@Service
public class OrderCreditMonthServiceImpl implements OrderCreditMonthService {

    @Resource
    private OrderCreditMonthDao ordercreditmonthDao;
    @Resource
    private ComputeService computeService;



    @Override
    public int getComputTimes(String id) {
        return ordercreditmonthDao.getComputTimes(id);
    }

    /**
     * 添加保存信息
     *
     * @param ordercreditmonth
     * @return ActionResult
     * @author 李爽
     * @date 2019-01-10
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public ActionResult saveOrderCreditMonth(OrderCreditMonth ordercreditmonth) {
        ActionResult actionResult = new ActionResult();
        ordercreditmonth.setAddtime(new Date());
        ordercreditmonthDao.saveOrderCreditMonth(ordercreditmonth);
        actionResult.setMessage("添加成功。");
        actionResult.setSuccess(CommonConstant.ACTIONRESULT_TRUE);

        return actionResult;
    }

    @Override
    public List<OrderCreditMonth> needComputeCommissionMonth(String monthStr) {
        return ordercreditmonthDao.needComputeCommissionMonth(monthStr);
    }

    @Override
    public void computerCommissionMonty() {
        String monthStr = IceDateFormatUtils.dateToString(new Date(),"yyyyMM");
        List<OrderCreditMonth> orderCreditMonths = needComputeCommissionMonth(monthStr);
        for (OrderCreditMonth orderCreditMonth :
                orderCreditMonths) {
            computeService.computeCommissionMonth(orderCreditMonth);
        }
    }

    @Override
    public List<OrderCreditMonth> getAllNeedPutOut() {
        return ordercreditmonthDao.getAllNeedPutOut();
    }

    @Override
    public void putOutOver(String id, Date putoutTime) {
        ordercreditmonthDao.putOutOver(id,putoutTime);
    }

}